<?php

class FinanceController extends Common {

    public function __construct() {
        parent::__construct();
        $this->load->model('order');

    }

    /**
     * 用户余额列表
     */
    public function listAction() {
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $model = $this->model('cbalance');
        $total = $model->count('cbalance', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))->select();
        $pagelist = $pagelist->total($total)->url(url('admin/finance/list', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/balance_list');  //这里是对视图文件的加载
    }

    /**
     * 用户资金流水记录
     */
    public function balanceInfoAction()
    {
        $uid = $this->get('uid')?:0;
        if(empty($uid)){echo '参数有误';}
        $model = $this->model('withdraw');
        $data = $model->getAll('uid=?',$uid,'*','id desc');  //用户资金流水记录
        $this->view->assign(array(
            'data' => $data,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/ubalance_list');  //这里是对视图文件的加载
    }

    /**
     * 充值
     */
    public function rechargeAction() {
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = "type = 1 and channel = 'recharge'";
        $model = $this->model('overflow');
        $total = $model->count('overflow', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->where($where)->page_limit($page, $pagesize)->order(array('id DESC'))->select();
        $pagelist = $pagelist->total($total)->url(url('admin/finance/recharge', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/recharge_list');  //这里是对视图文件的加载
    }

    /**
     * 提现
     */
    public function withdrawAction() {
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $model = $this->model('withdraw');
        $total = $model->count('withdraw', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))->select();
        $pagelist = $pagelist->total($total)->url(url('admin/finance/withdraw', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/withdraw_list');  //这里是对视图文件的加载
    }

    /**
     * 提现操作
     */
    public function operateWithdrawAction()
    {
        $result = ["code"=>0,"msg"=>"成功","data"=>[]];
        $id = $this->post('id');    //ca_withdraw id
        $operate = $this->post('operate');    //操作  confirm：确认  refuse：驳回
        if($operate=='confirm'){
            $status = 2;
        }elseif($operate=='refuse'){
            $status = 3;
        }else{
            $result["code"] = 1;
            $result["msg"] = "参数有误";
            echo json_encode($result);exit;
        }
        $this->load->model('cash');
        $withdrawInfo = $this->cash->getwithdrawInfo($id); //根据ca_withdraw id 获取用户uid
        if(empty($withdrawInfo->uid)){
            $result["code"] = 1;
            $result["msg"] = "查无记录";
            echo json_encode($result);exit;
        }
        if($withdrawInfo->status != 1){
            $result["code"] = 1;
            $result["msg"] = "该提现申请已处理，无需操作";
            echo json_encode($result);exit;
        }
        $data = [
            'id'=>$withdrawInfo->id,    //ca_withdraw id
            'uid'=>$withdrawInfo->uid,  //用户id
            'cash'=>$withdrawInfo->cash,    //提现金额
            'status'=>$status,  //1：申请提现	 2：提现成功	3：提现失败
            'account'=>$withdrawInfo->account,  //提现账户
        ];
        $accountInfo =$this->cash->operateWithdrawCash($id,$data);	//获取用户绑定支付宝信息
        if(empty($accountInfo)){
            $result["code"] = 1;
            $result["msg"] = "操作失败";
            echo json_encode($result);exit;
        }
        echo json_encode($result);
    }


    /**
     * 用户收益列表
     */
    public function incomeListAction() {
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $this->load->model('advertisement_overflow');
        $model = $this->model('advertisement_overflow');
        $total = $this->advertisement_overflow->getCount();
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $this->advertisement_overflow->getGroupData(($page-1)*10,$pagesize);
        $pagelist = $pagelist->total($total)->url(url('admin/finance/incomeList', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/income_list');  //这里是对视图文件的加载
    }

    /**
     * 用户个人收益列表
     */
    public function incomeInfoAction() {
        $uid = $this->get('uid');
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $this->load->model('advertisement_overflow');
        $model = $this->model('advertisement_overflow');
        $total = $this->advertisement_overflow->getIncomeInfoCount($uid);
        $urlparam = array(
            'page' => '{page}',
            'uid' => $uid,
        );
        $data = $this->advertisement_overflow->getIncomeInfoData(($page-1)*10,$pagesize,$uid);
//        var_dump($total,$data);exit;
        $pagelist = $pagelist->total($total)->url(url('admin/finance/incomeInfo', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/income_info');  //这里是对视图文件的加载
    }

    public function sfAction(){
        $username=$_POST[username];   //获取视图文件传过来的表单的值  表单我用post传值
        // $model = $this->model('member');  //实例化model 这里的两行是用fine的原有的写的，但是我感觉这样不灵活我下面又用php的写了因为我感觉还是php写的顺手，大家自己感觉什么好就用什么吧
        // $data = $model->getAll('username=?', "$username"); //用的fine的getall函数
        $sql="select * from fn_member where username='$username' ";   //sql语句  这里是php的
        $row=mysql_query($sql);  //php执行sql
        $data=mysql_fetch_assoc($row);  //获取sql语句得到的值
        $this->view->assign('data',$data); //assign()对视图变量完成赋值
        $this->view->display('sf'); //加载视图文件
    }

}
